using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Member_StockManage_DepotListManage : Member, IRequiresSessionState
{
	private string linkHead = "";
	private string beforeUrl = "DepotListManage.aspx?1=1";
	private string stockOrderType = "";
	private string stocktypeWhere = "";
	private int infoOrderID = 0;
	protected Literal ltrTitle;
	protected TextBox txtSearchBeginDate;
	protected TextBox txtSearchEndDate;
	protected TextBox txtSearchInfoNumber;
	protected Button btnSearch;
	protected Repeater rptList;
	protected Literal ltrPagePanel;
	protected Panel pnlManage;
	protected Label lblStockNumber;
	protected Label lblStockTypeC;
	protected Label lblStockTypeStatue;
	protected Label lblInputTime;
	protected Label lblInputer;
	protected Label lblMoneyTotalC;
	protected Label lblStatus;
	protected Label lblRemarks;
	protected GridView gvItem;
	protected Panel pnlDetail;
	public string StockOrderType
	{
		get
		{
			if (base.Request.QueryString["stockType"] != null)
			{
				this.stockOrderType = base.Request.QueryString["stockType"].Trim();
			}
			return this.stockOrderType;
		}
	}
	public string StocktypeWhere
	{
		get
		{
			if (this.StockOrderType != "")
			{
				this.stocktypeWhere = " and  (StockOrderType='" + this.StockOrderType + "') ";
			}
			return this.stocktypeWhere;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.OwnerStockPower(base.user.UserID))
			{
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(base.ActionTabInfo);
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (this.StockOrderType == "bad")
		{
			this.ltrTitle.Text = "报损单管理";
			this.lblStockTypeC.Text = "出库类型：";
		}
		else
		{
			if (this.StockOrderType == "baoyi")
			{
				this.ltrTitle.Text = "报溢单管理";
				this.lblStockTypeC.Text = "入库类型：";
			}
		}
		if (DisplayName != null)
		{
			if (DisplayName == "checkinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlDetail.Visible = true;
				if (this.InfoOrderID > 0)
				{
					this.InitPage();
				}
				return;
			}
		}
		this.pnlManage.Visible = true;
		this.pnlDetail.Visible = false;
		this.BindData();
	}
	protected void InitPage()
	{
		HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
		if (model != null)
		{
			this.lblStockNumber.Text = model.InfoNumber;
			this.lblInputTime.Text = model.CreateDate.ToString();
			this.lblInputer.Text = model.Inputer;
			this.lblMoneyTotalC.Text = model.TotalMoney.ToString();
			this.lblRemarks.Text = model.Remark;
			this.lblStatus.Text = BLLInstance.BllStockInfoOrder.GetStockStatusStr(model.StockStatus);
			if (!string.IsNullOrEmpty(model.StocktTypeID.ToString()))
			{
				int stockTypeID = Convert.ToInt32(model.StocktTypeID);
				HoWave.Data.Model.StockType model2 = BLLInstance.BllStockType.GetModel(stockTypeID);
				if (model2 != null)
				{
					this.lblStockTypeStatue.Text = model2.StockTypeName;
				}
			}
		}
		DataTable listInfoItemProductByInfoOrderID = BLLInstance.BllStockInfoOrder.GetListInfoItemProductByInfoOrderID(this.InfoOrderID);
		this.gvItem.DataSource = listInfoItemProductByInfoOrderID;
		this.gvItem.DataBind();
		for (int i = 0; i < this.gvItem.Rows.Count; i++)
		{
			string text = this.gvItem.Rows[i].Cells[2].Text;
			string text2 = this.gvItem.Rows[i].Cells[3].Text;
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
			{
				this.gvItem.Rows[i].Cells[5].Text = (Convert.ToInt32(text) * Convert.ToDecimal(text2)).ToString();
			}
		}
	}
	protected string GetQuery()
	{
		string text = " OwnerID=" + base.user.UserID + this.StocktypeWhere;
		string text2 = Fetch.Get("BeginTime");
		if (!string.IsNullOrEmpty(text2))
		{
			this.txtSearchBeginDate.Text = text2;
		}
		else
		{
			text2 = this.txtSearchBeginDate.Text;
		}
		if (!string.IsNullOrEmpty(text2))
		{
			text = text + " and DATEDIFF(DD, StockDate,'" + text2 + "')<=0";
			this.linkHead = this.linkHead + "&BeginTime=" + text2;
		}
		string text3 = Fetch.Get("EndTime");
		if (!string.IsNullOrEmpty(text3))
		{
			this.txtSearchEndDate.Text = text3;
		}
		else
		{
			text3 = this.txtSearchEndDate.Text;
		}
		if (!string.IsNullOrEmpty(text3))
		{
			if (!string.IsNullOrEmpty(text2))
			{
				if (Convert.ToDateTime(text2) > Convert.ToDateTime(text3))
				{
					this.WriteErrMsg("", Language.Get("Common/DateTimeErr"), "");
				}
			}
			text = text + "and DATEDIFF(DD, StockDate,'" + text3 + "')>=0";
			this.linkHead = this.linkHead + "&EndTime=" + text3;
		}
		string text4 = Fetch.Get("InfoNumber");
		if (!string.IsNullOrEmpty(text4))
		{
			this.txtSearchInfoNumber.Text = text4;
		}
		else
		{
			text4 = this.txtSearchInfoNumber.Text;
		}
		if (!string.IsNullOrEmpty(text4))
		{
			text = text + " and InfoNumber like '%" + text4 + "%'";
			this.linkHead = this.linkHead + "&InfoNumber=" + text4;
		}
		return text;
	}
	private string GetQueryByOrder()
	{
		string result = "";
		string value = this.txtSearchBeginDate.Text.Trim();
		string value2 = this.txtSearchEndDate.Text.Trim();
		if (!string.IsNullOrEmpty(value) || !string.IsNullOrEmpty(value2))
		{
			result = "StockDate desc";
		}
		return result;
	}
	public void BindData()
	{
		string query = this.GetQuery();
		string queryByOrder = this.GetQueryByOrder();
		int num = 0;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		int num2 = 10;
		int recordCount = BLLInstance.BllStockInfoOrder.GetRecordCount(query);
		int startIndex = (num - 1) * num2 + 1;
		int endIndex = num * num2;
		DataTable dataSource = BLLInstance.BllStockInfoOrder.GetListByPage(query, queryByOrder, startIndex, endIndex).Tables[0];
		this.rptList.DataSource = dataSource;
		this.rptList.DataBind();
		decimal d = 0m;
		for (int i = 0; i < this.rptList.Items.Count; i++)
		{
			Label label = (Label)this.rptList.Items[i].FindControl("lblMoney");
			if (label != null)
			{
				if (!string.IsNullOrEmpty(label.Text))
				{
					decimal d2 = Convert.ToDecimal(label.Text);
					d += d2;
				}
			}
			HiddenField hiddenField = (HiddenField)this.rptList.Items[i].FindControl("hfStockStatus");
			HyperLink hyperLink = (HyperLink)this.rptList.Items[i].FindControl("hlEdit");
			LinkButton linkButton = (LinkButton)this.rptList.Items[i].FindControl("lbtnConfirm");
			if (hiddenField != null)
			{
				if (hiddenField.Value.Trim() == "1" || hiddenField.Value.Trim() == "3")
				{
					hyperLink.Visible = false;
					linkButton.Visible = false;
				}
			}
		}
		Label label2 = (Label)this.rptList.Controls[this.rptList.Controls.Count - 1].FindControl("lblFootMoney");
		label2.Text = d.ToString();
		string url = string.Concat(new string[]
		{
			this.beforeUrl,
			"&stockType=",
			this.StockOrderType,
			this.linkHead,
			"&pageindex"
		});
		this.ltrPagePanel.Text = CurrPageLinks.instance.CurrPageLinkStr(num, num2, recordCount, "条记录&nbsp;每页" + num2 + "条记录&nbsp;", url);
	}
	protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
	{
		string comeUrl = this.beforeUrl + "&stockType=" + this.StockOrderType + this.linkHead;
		string commandName = e.CommandName;
		if (commandName == "DelInfo")
		{
			string value = e.CommandArgument.ToString();
			if (!string.IsNullOrEmpty(value))
			{
				if (this.StockOrderType == "bad")
				{
					bool flag = this.DelStockInfoAndItem(Convert.ToInt32(value), this.StockOrderType);
					if (flag)
					{
						this.WriteSuccessMsg("", "信息删除成功！", comeUrl);
					}
					else
					{
						this.WriteErrMsg("", "信息删除失败！", comeUrl);
					}
				}
			}
		}
		else
		{
			if (commandName == "ConfirmInfo")
			{
				string text = e.CommandArgument.ToString();
				if (!string.IsNullOrEmpty(text))
				{
					string[] array = text.Split(new string[]
					{
						","
					}, StringSplitOptions.RemoveEmptyEntries);
					if (array.Length > 1)
					{
						string str = array[0].Trim();
						string str2 = array[1].Trim();
						base.Response.Redirect("DepotList.aspx?stockType=" + str + "&Action=confirminfo&infoOrderID=" + str2);
					}
				}
			}
		}
	}
	private bool DelStockInfoAndItem(int infoid, string stockOrderType)
	{
		bool flag = false;
		if (infoid > 0)
		{
			HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(infoid);
			if (model != null)
			{
				DataTable dataTable = BLLInstance.BllStockItemOrder.GetList("InfoOrderID=" + infoid).Tables[0];
				foreach (DataRow dataRow in dataTable.Rows)
				{
					int num = Convert.ToInt32(dataRow["ItemOrderID"].ToString());
					int num2 = Convert.ToInt32(dataRow["ProductID"].ToString());
					int num3 = Convert.ToInt32(dataRow["StockCount"].ToString());
					if (num > 0)
					{
						flag = BLLInstance.BllStockItemOrder.Delete(num);
						if (flag)
						{
							if (model.StockOrderType == "bad")
							{
								if (model.StockStatus == 3)
								{
									string sql = string.Concat(new object[]
									{
										"update HLShop_Product set Stocks=Stocks+",
										num3,
										" where ProductID=",
										num2
									});
									this.conn.Execute(sql);
								}
							}
						}
					}
				}
				flag = BLLInstance.BllStockInfoOrder.Delete(infoid);
			}
		}
		return flag;
	}
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		this.BindData();
	}
}
